<?php

/**
 * Register widgetized areas, including two sidebars and widget-ready columns in the footer.
 *
 * To override lusi_widget() in a child theme, remove the action hook and add your own
 * function tied to the init hook.
 *
 * @uses register_sidebar
 */

//  Create Widget 
function algc_widget() {
    // Register Widget for Admin (Back-end)
    register_sidebar( array(
        
        'name' => __( 'Top Sidebar', 'algc' ),
        'id' => 'top-widget-area',
        'description' => __( 'Top Sidebar', 'algc' ),
		'before_widget' => '',
		'after_widget' => "",
    ) );
    
    register_sidebar( array(
        'name' => __( 'Google Map Sidebar', 'algc' ),
        'id' => 'google-map-widget-area',
        'description' => __( 'Google Map Sidebar', 'algc' ),
		'before_widget' => '',
		'after_widget' => "",
    ) );
    
    register_sidebar( array(
        'name' => __( 'HomeBox Sidebar', 'algc' ),
        'id' => 'homebox-widget-area',
        'description' => __( 'HomeBox Sidebar', 'algc' ),
		'before_widget' => '',
		'after_widget' => "",
    ) );
    
    register_sidebar( array(
        'name' => __( 'BeOurPartner Sidebar', 'algc' ),
        'id' => 'beourpartner-widget-area',
        'description' => __( 'BeOurPartner Sidebar', 'algc' ),
		'before_widget' => '',
		'after_widget' => "",
    ) );
    
    register_sidebar( array(
        'name' => __( 'Footer Sidebar', 'algc' ),
        'id' => 'footer-widget-area',
        'description' => __( 'Footer Sidebar', 'algc' ),
		'before_widget' => '',
		'after_widget' => "",
    ) );
}
add_action( 'widgets_init', 'algc_widget' );

// Widget
/********************************* Social Widget **************************************/
class Social_Widget extends WP_Widget{
    
    public function Social_Widget(){
        $widget_ops = array('description'=>'Social Link');
		$control_ops = array('width' => 400, 'height' => 300);
        parent::WP_Widget(false,$name='Social Link',$widget_ops,$control_ops);
    }
    
    /* Create widget form */
    public function form($instance){
        //Defaults
        $instance = wp_parse_args( (array) $instance, array('linkfacebook'=>'', 'linktwitter'=>'', 'skype'=>'') );
 
        $linkfacebook = htmlspecialchars($instance['linkfacebook']);
        $linktwitter = htmlspecialchars($instance['linktwitter']);        
        $skype = htmlspecialchars($instance['skype']);
        # linkfacebook
        echo '<p><label for="' . $this->get_field_id('linkfacebook') . '">' . 'Link Facebook:' . '</label><input class="widefat" id="' . $this->get_field_id('linkfacebook') . '" name="' . $this->get_field_name('linkfacebook') . '" type="text" value="' . $linkfacebook . '" /></p>';
        # linktwitter
        echo '<p><label for="' . $this->get_field_id('linktwitter') . '">' . 'Link Twitter:' . '</label><input class="widefat" id="' . $this->get_field_id('linktwitter') . '" name="' . $this->get_field_name('linktwitter') . '" type="text" value="' . $linktwitter . '" /></p>';
        # skype
        echo '<p><label for="' . $this->get_field_id('skype') . '">' . 'Skype:' . '</label><input class="widefat" id="' . $this->get_field_id('skype') . '" name="' . $this->get_field_name('skype') . '" type="text" value="' . $skype . '" /></p>';
    }
    
    /* Update widget */
    public function update($new_instance, $old_instance){
        $instance = $old_instance;
        $instance['linkfacebook'] = stripslashes($new_instance['linkfacebook']);
        $instance['linktwitter'] = stripslashes($new_instance['linktwitter']);
        $instance['skype'] = stripslashes($new_instance['skype']);
		
        return $instance;
    }
    
     /* Displays the Widget in the front-end */
    public function widget($args, $instance){
        extract($args);
        
        $linkfacebook = empty($instance['linkfacebook']) ? '' : $instance['linkfacebook'];
        $linktwitter = empty($instance['linktwitter']) ? '' : $instance['linktwitter'];
        $skype = empty($instance['skype']) ? '' : $instance['skype'];
        ?>
            <li><a href="<?php echo ($linkfacebook!='')? $linkfacebook : '#'; ?>" target="_blank"><img src="<?php bloginfo('template_url'); ?>/images/ico-fb.png" alt="facebook" /></a></li>
            <li><a href="<?php echo ($linktwitter!='') ? $linktwitter : '#'; ?>" target="_blank"><img src="<?php bloginfo('template_url'); ?>/images/ico-twitter.png" alt="twitter" /></a></li>
            <li>
                <script type="text/javascript" src="http://download.skype.com/share/skypebuttons/js/skypeCheck.js"></script>
                <a href="skype:<?php echo ($skype!='') ? $skype : '#'; ?>?call" target="_blank"><img src="<?php bloginfo('template_url'); ?>/images/ico-call-me.png" alt="call-me" /></a>
            </li>
        <?php
    }
}

add_action( 'widgets_init', create_function( '', 'register_widget( "Social_Widget" );' ) );
/////////////////////////////End widget//////////////////////////////////////////

/* Register Featured Image */
if ( function_exists( 'add_theme_support' ) ) {
    add_theme_support( 'post-thumbnails' );
    set_post_thumbnail_size( 375, 600, TRUE ); // default Post Thumbnail dimensions
    add_image_size( 'banner', 593, 366, true );
    add_image_size( 'histoire', 560, 414, true );
    add_image_size( 'transmission', 578, 434, true );
    add_image_size( 'sticky', 478, 665, true );
}

/*******************Get top level category*************************************/
function pa_category_top_parent_id ($catid) {
 while ($catid) {
  $cat = get_category($catid); // get the object for the catid
  $catid = $cat->category_parent; // assign parent ID (if exists) to $catid
  $catParent = $cat->cat_ID;
 }
return $catParent;
}


// Do shortcode
 add_filter('the_content', 'do_shortcode', 11);
// Register for using menu
add_theme_support('menus');
// Add class to active menu
function active_menu($output) {
  $output = preg_replace('/current-menu-item/', 'active current-menu-item', $output, 1);
  return $output;
}
add_filter('wp_nav_menu', 'active_menu');

function active_menu_parent($output) {
  $output = preg_replace('/current-menu-parent/', 'active current-menu-parent', $output, 1);
  return $output;
}
add_filter('wp_nav_menu', 'active_menu_parent');

// Google map for single post 
if (function_exists('register_field')) {
    register_field('Location_field', dirname(__FILE__) . '/location.php');
}
//Add first/last class in menu item
function add_first_and_last($output) {
  $output = preg_replace('/class="(\w*\s)?menu-item/', 'class="$1first menu-item', $output, 1);
  $pos=strripos($output, 'class="menu-item');
  $len=strlen('class="menu-item');
  $rep='class="last menu-item';
  
  if(strripos($output, ' menu-item ')>$pos){
	  $pos=strripos($output, ' menu-item ');
	  $len=strlen(' menu-item ');
	  $rep=' last menu-item ';
  }
  $output = substr_replace($output, $rep, $pos, $len);
  return $output;
}
add_filter('wp_nav_menu', 'add_first_and_last');

?>